import {bus} from '@beshell/events-ts/bus'
import { conn } from '@beshell/conn-ts/Connection'
import {AppModel} from '../AppModel'
import { PartSlaver } from './PartBase'
import { Cable } from '../conn/Cable'
import config from './config/BeJoystick.json'

export class BeJoystick extends PartSlaver {

    constructor(_model: AppModel){
        super(_model, config)

        this.handleWatchRemoteData = (device:string, data: any)=>{
        }
        bus.on('remote.sensor.data', this.handleWatchRemoteData)
    }

    destroy() {
        super.destroy()

        if(this.handleWatchRemoteData) {
            bus.off('remote.sensor.data', this.handleWatchRemoteData)
            this.handleWatchRemoteData = null
        }
    }

    private handleWatchRemoteData: any = null

    
    serial(): Cable | undefined {
        return undefined
    }

    public begin() {
        return true
    }

    public async remoteRead() {
        if(!conn.isConnected)
            return
        let color = await conn.console.call(`xx`)
        if(!color)
            return
        return color
    }

    public remoteWatch() {
        if(!conn.isConnected)
            return
        conn.console.run(`
        ${this.remoteVar()}.start()`)
    }

    public remoteUnwatch() {
        if(!conn.isConnected)
            return
        conn.console.run(`${this.remoteVar()}.stop()`)
    }
    
    public generateSetupCode(indent=0){
        return `${"\t".repeat(indent)}${this.remoteVar()} = new be.driver.BeJoystick()`
    }

    public generateBeginCode(indent=0){
        let serial = this.serial()
        if(serial && serial.isValid()) {
            return `${"\t".repeat(indent)}${this.remoteVar()}.begin( ${serial.name} )`
        }
        else {
            return ""
        }
    }
}